@extends('layouts.layouts')
@section('title')
    <title>后台首页</title>
@endsection
@section('content')
    <link rel="stylesheet" href="/lib/layer/theme/default/layer.css"/>
    <script type="text/javascript" src="/lib/layer/layer.js"></script>
    <div class="content">
        <div class="header">
            <ul class="breadcrumb">
                <li><a href="/article/list">文章管理</a></li>
                <li class="active">文章列表</li>
            </ul>
        </div>
        <div class="main-content">
            <div class="btn-toolbar list-toolbar">
                <form class="form-inline col-sm-4" action="" method="get">
                    <a href="/article/add"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" ><i class="fa fa-plus"></i> 新增</button></a>
                    <div class="form-group">
                        <input type="text" class="form-control" name="title" placeholder="课程标题" value="{{$title?$title:""}}">
                    </div>
                    <input type="hidden" name="is_video" value="{{$is_video}}" />
                    <button type="submit" class="btn btn-default">筛选</button>
                </form>
                <ul class="nav nav-pills">
                    <li role="presentation" class="{{$is_video?'':'active'}} commonCourse"><a href="/article/list">图文</a></li>
                    <li role="presentation" class="{{$is_video?'active':''}}"><a class="liveCourse" href="/article/list?is_video=1">视频</a></li>
                </ul>
            </div>
            

            <div class="row">
                <div class="col-sm-12 col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading no-collapse">课程列表</div>
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>编号</th>
                                    <th>文章名称</th>
                                    <th>封面图</th>
                                    <th>所属类别</th>
                                    <th>所属标签</th>
                                    <th>精选日期</th>
                                    <th>排序值</th>
                                    <th>作者</th>
                                    <!-- <th>最后操作者</th> -->
                                    <th>更新时间</th>
                                    <th>操作</th>
                                    <th>前端显示</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($list as $v)
                                <tr id="articleTr_{{$v->id}}">
                                    <td>{{$v->id}}</td>
                                    <td><a href="{{env('WEB_URL')}}article/detail/{{$v->id}}.html">{{$v->title}}</a></td>
                                    <td><img class="pimg" height='80' src="{{env('IMG_URL')}}{{$v->cover_url}}" /></td>
                                    <td>{{article_types($v->type_ids)}}</td>
                                    <td>{{article_tags($v->tag_ids)}}</td>
                                     <td>{{$v->today_selected}}</td>
                                    <td>{{$v->orderby}}</td>
                                    <td>{{$v->author?$v->author->name:''}}</td>
                                    <!-- <td>--</td> -->
                                    <td>{{$v->created_at}}</td>
                                    <td>
                                        <a class="btn btn-success btn-xs" href="/article/edit?id={{$v->id}}">编辑</a>
                                        <button class="btn btn-danger btn-xs" data-id="{{$v->id}}" onclick="articleDelete(this)">删除</button>
                                        <button class="btn btn-info btn-xs" data-id="{{$v->id}}" data-state="{{$v->state}}" onclick="articleState(this)">{{$v->state?'下架':'上架'}}</button>
                                    </td>
                                    <td>
                                        <button class="btn btn-info btn-xs" data-id="{{$v->id}}" data-is_index="{{$v->is_index}}" onclick="articleIndex(this)">{{$v->is_index?'首页显示':'不显示'}}</button>
                                        <button class="btn btn-info btn-xs" data-id="{{$v->id}}" data-is_selected="{{$v->is_selected}}" onclick="articleSelected(this)">{{$v->is_selected?'精选':'不精选'}}</button>
                                        <!-- <a class="btn btn-success btn-xs" href="/article/addcomment?id={{$v->id}}">添加评论</a> -->
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            {{$list->appends(['title'=>$title,"is_video"=>$is_video])->render()}}
        </div>
    </div>
    <script type="text/javascript">
        //修改文章状态
        function articleState(obj){
            var id = $(obj).attr('data-id');
            var state = $(obj).attr('data-state');
            if(state == 1){
                var notice = '确定下架吗？';
                var newState = 0;
                var name = '上架'
            }else{
                var notice = '确定上架吗？';
                var newState = 1;
                var name = '下架';
            }
            var _token = '{{csrf_token()}}';
            var data = {id:id,state:newState,_token:_token};
            var object = obj;
            layer.confirm(notice, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/article/changestate',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).attr('data-state',newState);
                            $(object).text(name);
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }

        //修改文章是否首页显示
        function articleIndex(obj){
            var id = $(obj).attr('data-id');
            var state = $(obj).attr('data-is_index');
            if(state == 1){
                var notice = '确定首页不显示吗？';
                var newState = 0;
                var name = '不显示'
            }else{
                var notice = '确定首页显示吗？';
                var newState = 1;
                var name = '首页显示';
            }
            var _token = '{{csrf_token()}}';
            var data = {id:id,state:newState,_token:_token};
            var object = obj;
            layer.confirm(notice, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/article/changeindex',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).attr('data-is_index',newState);
                            $(object).text(name);
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }

        //修改文章是否精选推荐
        function articleSelected(obj){
            var id = $(obj).attr('data-id');
            var state = $(obj).attr('data-is_selected');
            if(state == 1){
                var notice = '确定不精选吗？';
                var newState = 0;
                var name = '不精选'
            }else{
                var notice = '确定精选吗？';
                var newState = 1;
                var name = '精选';
            }
            var _token = '{{csrf_token()}}';
            var data = {id:id,state:newState,_token:_token};
            var object = obj;
            layer.confirm(notice, {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/article/changeselected',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $(object).attr('data-is_selected',newState);
                            $(object).text(name);
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });
        }
        function articleDelete(obj){
            var id = $(obj).attr('data-id');
            var _token = '{{csrf_token()}}';
            var data = {id:id,_token:_token};
            layer.confirm('确定删除吗？', {
                btn: ['是', '否'],
            }, function(index){
                $.ajax({
                    url:'/article/delete',
                    data:data,
                    type:'POST',
                    dataType: 'json',
                    success:function(res){
                        layer.alert(res.message);
                        if(res.code == 0){
                            $("#articleTr_"+id).remove();
                        }

                    }
                });
                layer.close(index);
                return true;
            }, function(index){
                layer.close(index);
            });

        }
    </script>
@endsection